package com.sunxd.brvahexpandableproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseNodeAdapter;
import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private MyAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setList(ModelLab.getInstance().getData());
    }

    private class MyAdapter extends BaseNodeAdapter {
        public MyAdapter() {
            addNodeProvider(new MaterialProvider());
            addNodeProvider(new SupplyDataProvider());
        }

        @Override
        protected int getItemType(@NotNull List<? extends BaseNode> list, int i) {
            BaseNode n = list.get(i);
            if (n instanceof Material) {
                return 1;
            } else if (n instanceof SupplyData) {
                return 2;
            } else {
                return 0;
            }
        }
    }

    private class MaterialProvider extends BaseNodeProvider {

        @Override
        public int getItemViewType() {
            return 1;
        }

        @Override
        public int getLayoutId() {
            return R.layout.list_item_material;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
            Material m = (Material)baseNode;
            baseViewHolder.setText(R.id.material_name, m.getName());
            baseViewHolder.setText(R.id.material_price, "" + m.getPrice());
        }

        @Override
        public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
            super.onClick(helper, view, data, position);
            mAdapter.expandOrCollapse(position);
        }
    }

    private class SupplyDataProvider extends BaseNodeProvider {

        @Override
        public int getItemViewType() {
            return 2;
        }

        @Override
        public int getLayoutId() {
            return R.layout.list_item_supply_data;
        }

        @Override
        public void convert(@NotNull BaseViewHolder baseViewHolder, BaseNode baseNode) {
            SupplyData s = (SupplyData)baseNode;
            baseViewHolder.setText(R.id.supply_name, s.getName())
                    .setText(R.id.supply_price, "" + s.getPrice())
                    .setText(R.id.supply_number, "" + s.getNumber())
                    .setText(R.id.supply_time, "" + s.getTime());
        }
    }
}